package cn.edu.lingnan.servlet;

import cn.edu.lingnan.pojo.Admin;
import cn.edu.lingnan.pojo.Project;
import cn.edu.lingnan.pojo.ProjectAdult;
import cn.edu.lingnan.pojo.User;
import cn.edu.lingnan.service.ProjectService;
import cn.edu.lingnan.service.ProjectServiceImp;
import cn.edu.lingnan.service.userservice;
import cn.edu.lingnan.service.userserviceimp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class LoginServlet extends HttpServlet {
    private ProjectService projectService = new ProjectServiceImp();


    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username + " " + password);


        userservice us=new userserviceimp();
        Admin admin = us.Login(username, password);
        List<ProjectAdult> projectAdults = projectService.queryaduitProject();
        List<Project> studnetCreateProject = projectService.QueryStudnetCreateProject();
        if(admin!=null){
            System.out.println("用户存在: " + admin);
            //重定向到首页
            req.getSession().setAttribute("admin",admin);
            req.getSession().setAttribute("projectAdults",projectAdults);
            req.getSession().setAttribute("studnetCreateProject",studnetCreateProject);
           // req.getSession().setAttribute("username",username);
           // req.getServletContext().setAttribute("username",username);
            resp.sendRedirect(req.getContextPath() + "/manager/main.jsp");
        }
        else{
            req.setAttribute("error","登录失败，请检查用户名或密码");
            req.getRequestDispatcher("/error.html").forward(req,resp);
        }

    }
}
